import datetime
from typing import Optional
from sqlalchemy import DateTime, Index, String, text
from sqlalchemy.dialects.mysql import INTEGER, TINYINT, VARCHAR, BIGINT
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Base(DeclarativeBase):
    pass


class EqInfo(Base):
    __tablename__ = 'eq_info'
    __table_args__ = (
        Index('mac_unique', 'mac', unique=True),
    )

    id: Mapped[int] = mapped_column(INTEGER(11), primary_key=True)
    mac: Mapped[str] = mapped_column(VARCHAR(255), comment='设备MAC地址')
    power_ontime: Mapped[int] = mapped_column(BIGINT(20), comment='设备上电时间')
    sim_card_num: Mapped[str] = mapped_column(String(255), comment='流量卡号')
    verification: Mapped[int] = mapped_column(INTEGER(8), comment='验证码')
    device_name: Mapped[Optional[str]] = mapped_column(String(255), comment='设备名称')
    add_time: Mapped[Optional[int]] = mapped_column(BIGINT(20), comment='插入时间')
    eq_type: Mapped[Optional[str]] = mapped_column(String(255), comment='设备类型')
    status: Mapped[Optional[int]] = mapped_column(TINYINT(4), comment='0为离线，1为在线')
    version: Mapped[Optional[str]] = mapped_column(String(255), comment='版本')
    cate: Mapped[Optional[str]] = mapped_column(String(255), comment='所属产品')
    source: Mapped[Optional[str]] = mapped_column(String(255), comment='来源')
    owner: Mapped[Optional[str]] = mapped_column(String(255), comment='所属主体')
    breeding_base: Mapped[Optional[str]] = mapped_column(String(255), comment='所属基地')
    plot: Mapped[Optional[str]] = mapped_column(String(255), comment='所属地块')

    def to_dict(self):
        return {
            'id': self.id,
            'mac': self.mac,
            'power_ontime': self.power_ontime,
            'add_time': self.add_time,
            'eq_type': self.eq_type,
            'status': self.status,
            'sim_card_num': self.sim_card_num,
            'version': self.version,
            'verification': self.verification,
            'cate': self.cate,
            'source': self.source,
            'owner': self.owner,
            'breeding_base': self.breeding_base,
            'plot': self.plot
        }

class UserInfo(Base):
    __tablename__ = 'user_info'
    __table_args__ = (
        Index('uk_mobile', 'mobile', unique=True),
        {'comment': '用户表'}
    )

    id: Mapped[int] = mapped_column(INTEGER(10), primary_key=True, comment='用户ID')
    username: Mapped[str] = mapped_column(String(50), comment='用户名')
    pwd: Mapped[str] = mapped_column(String(255), comment='密码')
    mobile: Mapped[str] = mapped_column(String(20), comment='手机号')
    create_time: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP'), comment='创建时间')
    update_time: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment='更新时间')
    role: Mapped[Optional[str]] = mapped_column(String(20), comment='角色')
    status: Mapped[Optional[int]] = mapped_column(TINYINT(4), server_default=text("'0'"), comment='状态(0:正常,1:禁用)')
    last_login_at: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, comment='最后登录时间')

    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'pwd': self.pwd,
            'mobile': self.mobile,
            'create_time': self.create_time,
            'update_time': self.update_time,
            'role': self.role,
            'status': self.status,
            'last_login_at': self.last_login_at,
        }

class EqCategory(Base):
    __tablename__ = 'eq_category'
    __table_args__ = (
        Index('uk_ident', 'category_ident', unique=True),
        {'comment': '设备分类表'}
    )

    id: Mapped[int] = mapped_column(INTEGER(10), primary_key=True, comment='主键ID')
    category_name: Mapped[str] = mapped_column(String(100), comment='分类名称')
    category_ident: Mapped[str] = mapped_column(String(50), comment='分类标识(唯一代码)')
    status: Mapped[int] = mapped_column(TINYINT(4), server_default=text("'1'"), comment='状态(1:启用 0:停用)')
    remark: Mapped[Optional[str]] = mapped_column(String(255), comment='备注说明')
    def to_dict(self):
        return {
            'id': self.id,
            'category_name': self.category_name,
            'category_ident': self.category_ident,
            'status': self.status,
            'remark': self.remark,
        }

class ProductManagement(Base):
    __tablename__ = 'product_management'
    __table_args__ = {'comment': '产品信息表'}

    id: Mapped[int] = mapped_column(INTEGER(10), primary_key=True, comment='产品ID')
    product_name: Mapped[str] = mapped_column(String(100), comment='产品名称')
    create_time: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP'), comment='创建时间')
    add_time: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP'), comment='添加时间')
    product_type: Mapped[Optional[str]] = mapped_column(String(100), comment='产品类型')
    image: Mapped[Optional[str]] = mapped_column(String(255), comment='产品图片URL')
    status: Mapped[Optional[int]] = mapped_column(TINYINT(4), comment='发布状态')

    def to_dict(self):
        return {
            'id': self.id,
            'product_name': self.product_name,
            'product_type': self.product_type,
            'image': self.image,
            'status': self.status,
            'create_time': self.create_time,
            'add_time': self.add_time,
        }


